package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.user.TruckDriverLicenseDto;
import com.itheima.pinda.entity.user.PdTruckDriverLicense;
import com.itheima.pinda.mapper.PdTruckDriverLicenseMapper;
import com.itheima.pinda.mapstruct.TruckDriverDTO2Entity;
import com.itheima.pinda.service.PdTruckDriverLicenseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class PdTruckDriverLicenseServiceImpl extends ServiceImpl<PdTruckDriverLicenseMapper, PdTruckDriverLicense> implements PdTruckDriverLicenseService {


    @Resource
    private TruckDriverDTO2Entity driverDTO2Entity;

    /**
     * 保存司机驾驶证信息
     *
     * @param dto 司机驾驶证信息
     * @return 返回信息
     */
    @Override
    public TruckDriverLicenseDto saveDriverLicense(TruckDriverLicenseDto dto) {
        PdTruckDriverLicense pdTruckDriverLicense = driverDTO2Entity.conver(dto);
        PdTruckDriverLicense result = super.getOne(Wrappers.<PdTruckDriverLicense>lambdaQuery()
                .eq(PdTruckDriverLicense::getUserId, pdTruckDriverLicense.getUserId()));

        if (result != null) {
            pdTruckDriverLicense.setId(result.getId());
        }
        super.saveOrUpdate(pdTruckDriverLicense);
        dto = driverDTO2Entity.conver(pdTruckDriverLicense);
        return dto;
    }
}
